/*
 * Copyright (c) 2009, Hamish Morgan
 * All rights reserver.
 */
package locusts.common.behaviours;

import javax.vecmath.Tuple2d;
import javax.vecmath.Vector2d;

import locusts.common.entities.Vehicle;

/**
 * Steering towards a target point.
 *
 * @author Hamish Morgan
 */
public class SeekBehaviour extends AbstractBehaviour {
    
    protected static final Tuple2d DEFAULT_SEEK_TARGET = null;
    protected Tuple2d target;
    
    public SeekBehaviour(double weight, Tuple2d target) {
	super(weight);	
	setTarget(target);
    }
    
    public SeekBehaviour(double weight) {
	this(weight, DEFAULT_SEEK_TARGET);
    }
    
    public SeekBehaviour() {
	this(DEFAULT_WEIGHT);
    }
    
    public void setTarget(Tuple2d target) {
        if (target == null)
            throw new IllegalArgumentException();
	this.target = target;
    }
    
    public void clearTarget() {
	this.target = null;
    }
    
    public Vector2d getSteering(Vehicle boid) {
	Vector2d steering = new Vector2d();
	if (getWeight() == 0)
	    return steering;
	if (target == null)
	    return steering;

	steering.sub(boid.position, target);
	steering.scale(getWeight());
	
	return steering;
    }
    
}
